package com.koron.css2.clientCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserInfoSplit;
import com.koron.css2.clientCenter.mapper.UserInfoSplitMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoSplitQuery;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class UserInfoSplitList implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoSplitQuery userInfoSplitQuery = JsonUtils.objectToPojo(req.getData(), UserInfoSplitQuery.class);
            UserInfoSplitMapper userInfoSplitMapper = factory.getMapper(UserInfoSplitMapper.class);

            // 1. 分页参数
            PageHelper.startPage(userInfoSplitQuery.getPage(), userInfoSplitQuery.getPageCount());
            // 2. 查询数据
            List<UserInfoSplit> userInfoSplitList = userInfoSplitMapper.getList(userInfoSplitQuery);
            userInfoSplitList.forEach(userInfoSplit -> {
                // 2.1 processState -> processStateName
                String statusName = Tools.transProcessStatus(userInfoSplit.getProcessState());
                userInfoSplit.setProcessStateName(statusName);
            });
            // 3. 返回数据
            MessageBean<PageInfo> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "拆户列表查询成功", null);
            messageBean.setData(new PageInfo(userInfoSplitList));

            return messageBean;
        } catch (Exception e) {
            logger.error("拆户列表查询失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "拆户列表查询失败", null);
        }
    }
}
